<?php  
$pPageIsPublic = false;
include '../_common.php';
	
$_SESSION["m1"]	= "content";
$_SESSION["m2"] = 'building-list';

$pTitle  = 'New Video Tour';
$pButton = 'Save';
$action  = 'add';
$id 	 = intval($_REQUEST['id']);
$bid 	 = intval($_REQUEST['bid']);
$seo	 = NULL;

if (!$bid)
	Tzn::redirect('building-add.php', 'Create a building.'); 

$objMedia = new File();
$objMedia ->setUid($id);
if ($objMedia ->load()) {
	$pTitle  = 'Edit Video Tour';
	$pButton = 'Update';
	$action  = 'edit';
}

if (isset($_POST['submit'])) {
	$objMedia ->setAuto($_POST);
	$objMedia ->page ->id = $bid;
	$objMedia ->video = youtubeId($objMedia ->video);
	  
	if ($objMedia ->video != $_POST['old'] ) {
		@unlink(TZN_FILE_UPLOAD_URL.$objMedia ->target);
		if (valid_youtube_id($objMedia ->video)) {
			$file = 'http://i4.ytimg.com/vi/'.$objMedia ->video.'/0.jpg';
			$objMedia ->target = save_img($file,'jpg');  
		} 
	}
	
	
	if ($_POST['action'] == 'edit') {
		$objMedia ->getConnection();
		$objMedia ->setUid($id);
		$objMedia ->update();
		Tzn::redirect('video-add.php?bid='. $bid . '&id='. $objMedia ->id, 'Video Tour has been updated.');
	} elseif ($_POST['action'] == 'add') {
		$objMedia ->add();
		Tzn::redirect('video-add.php?bid='. $bid . '&id='. $objMedia ->id, 'Video Tour has been added.');
	}
}
?>
<?php include("header.php"); ?>
<script type="text/javascript">
$(document).ready(function(){
	$('#frmMain').validate();
});
</script>
<?php include("sidebar.php"); ?>
<ul id="subnav" >
	<li ><a class="back" href="<?="building-add.php?tab=video&id=$bid"; ?>" > Back </a></li>
	<li class="noborder" >
		<a class="add" href="<?="video-add.php?bid=$bid"?>" > Add a New Video Tour </a>
	</li>
</ul>
<div id="list">
<h2> <?php echo $pTitle;?> </h2>  
<form action="" method="post" name = "frmMain" id = "frmMain" enctype="multipart/form-data" >
<fieldset><legend> General Info </legend>
	<table cellpadding="0" cellspacing="0" class="table-form"  >
		<tr>
			<th>Title: </th>
					<td><?php $objMedia ->qText('title', '', 'width:548px', 'class="required"');?></td>
				</tr>
				<tr>
				<th>Video: </th>
					<td><?php
					$yt = valid_youtube_id($objMedia ->video);
					if ($objMedia ->video && $yt)
						print '<div style="width:268px;margin-bottom:4px;" class="img-border">
							<iframe src="http://www.youtube.com/embed/'. $objMedia ->video .'?rel=0&hd=1&controls=0&showinfo=0" width="268" height="152" frameborder="0"></iframe></div>';
					
					Tzn::qText('video', 'http://www.youtube.com/watch?v=' . $objMedia ->video, 'width:330px;', 'class="required"');
					print !$yt ? '<label class="error">Youtube Video ID Inavlid.</label>' : '';
					Tzn::qHidden('old', $objMedia ->video);  ?>
					</td>
				</tr>
				<tr>
					<th>Status: </th>
					<td><?php $objMedia ->arrayToSelect($objMedia ->_arrStatus, 'status', $objMedia ->status, false, 'width:100px', '');?></td>
				</tr>
			</table>
	</fieldset>
	<div style="margin-top:10px">
		<input type="hidden" name="id" value="<?php echo $id; ?>" />
		<input type="hidden" name="bid" value="<?php echo $bid; ?>" />
		<input type="hidden" name="action" value="<?php echo $action; ?>" />
                <input type="submit" name="submit"  value="<?php echo $pButton; ?>" /> &nbsp; <input type="reset" value="Cancel" onClick="window.location.href='<?="building-add.php?tab=video&id=$bid"; ?>'" />
            </div>
            </form>
</div>
<?php include("footer.php"); ?>